package com.minrray.mediaplayerdemo;

import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MediaPlayerDemo";
    private EditText mUrlEditText;
    private VideoView mVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mUrlEditText = (EditText)findViewById(R.id.editText);
        mVideoView = (VideoView)findViewById(R.id.videoView);

        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d(TAG, "onError what: " + what + " extra: " + extra);
                mVideoView.stopPlayback();
                return false;
            }
        });

        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.d(TAG, "onPrepared");
                mVideoView.start();
            }
        });

        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d(TAG, "onCompletion");
                Stop(null);
                Play(null);
            }
        });
    }

    public void Play(View view) {
        Uri uri = Uri.parse(mUrlEditText.getText().toString());
        Log.d(TAG, "video uri: "+ uri);
        mVideoView.setVideoURI(uri);
        mVideoView.getHolder().getSurfaceFrame();
        //mVideoView.start();
    }

    public void Stop(View view) {
        mVideoView.stopPlayback();
    }


}
